#include<bits/stdc++.h>
#define int long long
using namespace std;
int f[35][2][2];
int a[35],b[35];
int dfs(int p,int fl1,int fl2)
{
	if(p<0) return 1;
	if(f[p][fl1][fl2]!=-1) return f[p][fl1][fl2];
	int lim1 = 1,lim2 = 1;
	if(!fl1) lim1 = a[p];
	if(!fl2) lim2 = b[p];
	int res = 0;
	for(int i = 0;i<=lim1;i++)
		for(int j = 0;j<=min(lim2,i);j++)
			res+=dfs(p-1,(fl1||i<lim1),(fl2||j<lim2)); 
	return f[p][fl1][fl2] = res;
}
inline int solve(int x,int y)
{
	for(int i = 30;~i;i--)
		a[i] = (x>>i)&1;
	for(int i = 30;~i;i--)
		b[i] = (y>>i)&1;
	memset(f,-1,sizeof f);
	return dfs(30,0,0);
}
signed main()
{
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	while(q--)
	{
		int l,r;
		cin>>l>>r;
		cout<<solve(r,r)-solve(r,l-1)<<'\n';
	}
	return 0;
}

